#include "sys_config.h"

#define TASK_STACK_SIZE     (1024*4)

Car_status globals_car_status;
Cmd_Now globals_cmd_now;
osMutexId_t globals_Mutex_ID = 0;
osMutexId_t uart_update_Mutex_ID = 0;

osThreadId_t Tcp_Recv_Tackid = 0;
osThreadId_t Uart_Recv_Tackid = 0;
osThreadId_t Car_Control_Tackid = 0;
osThreadId_t Tcp_Send_Tackid = 0;

osThreadId_t Udp_Recv_Tackid = 0;

int nfc_connect_wifi_init(void)
{
    /********************************* NFC碰一碰联网 *********************************/
    uint8_t ndefLen = 0;      // ndef包的长度
    uint8_t ndef_Header = 0;  // ndef消息开始标志位-用不到
    uint32_t result_code = 0; // 函数的返回值
    // 读整个数据的包头部分，读出整个数据的长度
    if (result_code = NT3HReadHeaderNfc(&ndefLen, &ndef_Header) != true) {
        printf("NT3HReadHeaderNfc is failed. result_code = %d\r\n", result_code);
        return -1;
    }
    ndefLen += NDEF_HEADER_SIZE; // 加上头部字节
    if (ndefLen <= NDEF_HEADER_SIZE) {
        printf("ndefLen <= 2\r\n");
        return -1;
    }
    
    uint8_t *ndefBuff = (uint8_t *)malloc(ndefLen + 1);
    if (ndefBuff == NULL) {
        printf("ndefBuff malloc is Falied!\r\n");
        return -1;
    }

    if (result_code = get_NDEFDataPackage(ndefBuff, ndefLen) != HI_ERR_SUCCESS) {
        printf("get_NDEFDataPackage is failed. result_code = %d\r\n", result_code);
        return -1;
    }

    printf("start print ndefBuff.\r\n");
    for (size_t i = 0; i < ndefLen; i++) {
        printf("0x%x ", ndefBuff[i]);
    }
    printf("\n");

    while (NFC_configuresWiFiNetwork(ndefBuff) != WIFI_SUCCESS) {
        printf("nfc connect wifi is failed!\r\n");
        oled_consle_log("wifi no.");
        sleep(1);
        SSD1306_CLS(); // 清屏
    }
    oled_consle_log("wifi yes.");
    return 0;
}

/**
 * @brief 初始化串口、oled、nfc、wifi、TCP服务器
 * @note {*}
 * @param {*}
 * @return {*}
 */
static void device_init(void)
{
    /********************************** 外设初始化 **********************************/
    PCF8574_Init();
    AW2013_Init(); // 三色LED灯的初始化
    AW2013_Control_Red(0);
    AW2013_Control_Green(0);
    AW2013_Control_Blue(0);
    SSD1306_Init(); // OLED 显示屏初始化
    SSD1306_CLS();  // 清屏
    // nfc_Init();     // NFC 初始化
    // nfc_connect_wifi_init();

    oled_show("Connect WIFI...", OLED_BUF_LEN, 0);
    configWifi();
    oled_show(g_IP_Addr, OLED_BUF_LEN, 0);
    strcpy(globals_car_status.local_ip_address, g_IP_Addr);
    
    globals_car_status.beep_state = BEEP_STATUS_OFF;
    globals_car_status.led_state = LED_STATUS_OFF;
    globals_car_status.driving_mode = DRIVING_MODE_MANUAL;
    globals_car_status.driving_state = DRIVING_STATUS_STOP;

    uart_init();//串口初始化
    Json_Enc_SendToUart(DRIVING_STATUS_ON, "car", 0); //开启PWM，启动电机
    while(UdpServer_Init());//创建UDP服务器
}

static void example_smartcar(void)
{
    printf("Enter example_smartcar()!\r\n");
    device_init();

        // 创建互斥锁
    globals_Mutex_ID = osMutexNew(NULL);
    if (globals_Mutex_ID != NULL) {
        printf("ID = %d, Create Mutex_ID is OK!\n", globals_Mutex_ID);
    }

    uart_update_Mutex_ID = osMutexNew(NULL);
    if (uart_update_Mutex_ID != NULL) {
        printf("ID = %d, Create Mutex_ID is OK!\n", uart_update_Mutex_ID);
    }

    osThreadAttr_t options;
    options.attr_bits = 0;
    options.cb_mem = NULL;
    options.cb_size = 0;
    options.stack_mem = NULL;
    options.stack_size = TASK_STACK_SIZE;
    
    options.name = "Udp_Recv_Tack";
    options.priority = osPriorityNormal1;
    Udp_Recv_Tackid = osThreadNew((osThreadFunc_t)Udp_Recv_Tack, NULL, &options);
    if (Udp_Recv_Tackid != NULL) {
        printf("ID = %d, Create Udp_Recv_Tack is OK!\r\n", Udp_Recv_Tackid);
    }

    options.name = "uart_recv_task";
    options.priority = osPriorityNormal;
    Uart_Recv_Tackid = osThreadNew((osThreadFunc_t)uart_recv_task, NULL, &options);
    if (Uart_Recv_Tackid != NULL) {
        printf("ID = %d, Create uart_recv_task is OK!\r\n", Uart_Recv_Tackid);
    }

    options.name = "Car_Control_Tack";
    options.priority = osPriorityNormal;
    Car_Control_Tackid = osThreadNew((osThreadFunc_t)Car_Control_Tack, NULL, &options);
    if (Car_Control_Tackid != NULL) {
        printf("ID = %d, Create Car_Control_Tack is OK!\r\n", Car_Control_Tackid);
    }


}
SYS_RUN(example_smartcar);